package com.jeesite.modules.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期计算类
 */
public class DateUtils {
	
	static Calendar c = Calendar.getInstance();

	/** 
	 * 得到几天前的时间 
	 *  
	 * @param d 
	 * @param day 
	 * @return 
	 */
	public static Date getDateBefore(Date d, int day) {
		c.clear();
		c.setTime(d);
		c.set(Calendar.DATE, c.get(Calendar.DATE) - day);
		return c.getTime();
	}

	/** 
	 * 得到几天后的时间 
	 *  
	 * @param d 
	 * @param day 
	 * @return 
	 */
	public static Date getDateAfter(Date d, int day) {
		c.clear();
		c.setTime(d);
		c.set(Calendar.DATE, c.get(Calendar.DATE) + day);
		return c.getTime();
	}

	/**
	 * 判断2个时间的大小
	 * @param args
	 */
	public static boolean compareDate(Date a, Date b) {
		if (a.getTime() >= b.getTime()) {
			System.out.println("a在b前");
			return true;
		} else {
			System.out.println("a在b后");
			return false;
		}
	}

	/**
	 * 获取当天的最后一秒日期
	 * @throws ParseException 
	 */
	public static Date getLastSecond(Date date) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sf.format(date) + " 23:59:59";
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当天的开始日期
	 * @throws ParseException 
	 */
	public static Date getFirstSecond(Date date) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sf.format(date) + " 00:00:00";
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取前一天的开始日期
	 * @throws ParseException 
	 */
	public static Date getYesterdayFirstSecond(Date date) {
		c.clear();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, -1);
		date = c.getTime();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sf.format(date) + " 00:00:00";
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取前一天的最后一秒日期
	 * @throws ParseException 
	 */
	public static Date getYesterdayLastSecond(Date date) {
		c.clear();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, -1);
		date = c.getTime();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sf.format(date) + " 23:59:59";
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	

}

